<?php
class User_AdminController extends Main_Controller_AdminAction
{
	const PAGER_NAME_ROLE = 'User_AdminRolePager';
	
	const PAGER_NAME_USER = 'User_AdminUserPager';
	
	/**
	 * @var App_User_Form_AdminRoleEdit
	 */
	private $_formAdminRoleEdit;
	
	/**
	 * @var App_User_Form_AdminUserEdit
	 */
	private $_formAdminUserEdit;
	
	/**
	 * @var App_User_Service_Role
	 */
	private $_serviceRole;
	
	/**
	 * @var App_User_Service_User
	 */
	private $_serviceUser;
	
	/**
	 * @return void
	 */
	public function init() 
	{
		$this->_formAdminRoleEdit = Main_Form::factory('User_Form_AdminRoleEdit');
		$this->_formAdminUserEdit = Main_Form::factory('User_Form_AdminUserEdit');
		$this->_serviceRole = new App_User_Service_Role();
		$this->_serviceUser = new App_User_Service_User(); 	
		
		parent::init();
	}
	
	// --- role --- {{
	
	/**
	 * @return render
	 */
	public function roleListAction()
	{
		return $this->render();
	}
	
	/**
	 * @return render
	 */
	public function roleAddAction() 
	{
		$form = $this->_formAdminRoleEdit;
		
		if ($this->_request->isPost()) {
			$data = $this->_getAllParams();
            if ($form->isValid($data)) {
 		 		$this->_serviceRole->insertItem($data);
 		 		return $this->_redirect('/user/admin/role.list' . Main_Const::getSeriveUrlStr());      		    			
            }
		}    
		
		$this->view->form = $form;
		return $this->render();
	}

	/**
	 * @return render
	 */
	public function roleEditAction() 
	{
		$roleId = $this->_getParam('id');
		
		$form = $this->_formAdminRoleEdit;
		
		if (! $this->_request->isPost()) {
			$dataDb = $this->_serviceRole->getItemById($roleId);
			$form->setFormValues($dataDb);
		} else {
			$data = $this->_getAllParams();
            if ($form->isValid($data)) {
 		 		$this->_serviceRole->updateItem($data, $roleId);
 		 		return $this->_redirect('/user/admin/role.list' . Main_Const::getSeriveUrlStr());      		    			
            }
		}   
		
		$this->view->form = $form;
		return $this->render();
	}

	/**
	 * @return json
	 */
	public function ajaxPagerRoleAction()
	{
	    if (!$this->_request->isXmlHttpRequest()) {
            $this->_response->setHttpResponseCode(404)->sendHeaders();
            return $this->_redirect('/error/error');
        }
        
     	$pager = Main_Pager::factory('User_Pager_AdminRole', self::PAGER_NAME_ROLE); 
     	
     	$result = $pager->getResponse($_REQUEST);

     	$this->_helper->json($result);   
	}
	
	// --- role --- }}
	
	// --- user --- {{
	
	/**
	 * @return render
	 */
	public function userListAction()
	{
		return $this->render();
	}

	/**
	 * @return render
	 */
	public function userAddAction() 
	{
		$form = $this->_formAdminUserEdit;
		$form->setupByAction('add');
		
		if ($this->_request->isPost()) {
			$data = $this->_getAllParams();
            if ($form->isValid($data)) {
 		 		$this->_serviceUser->insertItem($data);
 		 		return $this->_redirect('/user/admin/user.list' . Main_Const::getSeriveUrlStr());      		    			
            }
		}    
		
		$this->view->form = $form;
		return $this->render();
	}
	
	/**
	 * @return render
	 */
	public function userEditAction() 
	{
		$userId = $this->_getParam('id');
		
		$form = $this->_formAdminUserEdit;
		
		if (! $this->_request->isPost()) {
			$dataDb = $this->_serviceUser->getItemById($userId);
			$form->setFormValues($dataDb);
		} else {
			$data = $this->_getAllParams();
            if ($form->isValid($data)) {
 		 		$this->_serviceUser->updateItem($data, $userId);
 		 		return $this->_redirect('/user/admin/user.list' . Main_Const::getSeriveUrlStr());      		    			
            }
		}   
		
		$this->view->form = $form;
		return $this->render();
	}
	
	/**
	 * @return json
	 */
	public function ajaxPagerUserAction()
	{
	    if (!$this->_request->isXmlHttpRequest()) {
            $this->_response->setHttpResponseCode(404)->sendHeaders();
            return $this->_redirect('/error/error');
        }
        
     	$pager = Main_Pager::factory('User_Pager_AdminUser', self::PAGER_NAME_USER); 
     	
     	$result = $pager->getResponse($_REQUEST);

     	$this->_helper->json($result);   
	}
	
	
	// --- user --- }}
}
